<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\controller\Common;
use app\service\couponService;
use app\service\apiService;
use app\service\parkingService;
use app\service\orderService;
use printing\printing;
use think\Db;
use think\db\Where;
use think\request;
class Coupon extends Controller{
    public function merchantList(){
        //根据当前账号查询区域
        $com = (new Common())->pidTokenId();
        $whe=new Where();
        $whe['community_id'] = array('in',implode(',',$com['community_id']));
        //根据账号查询停车场
        $parkinglist = (new orderService())->parkingList($whe);
        $this->assign('parkinglist',$parkinglist);
        //查询停车场ID
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $parkingNo[] = $park['parkingNo'];
            }
            //将数组转换成字符串
            $parkingNo = implode(',',$parkingNo);
        }else{
            $parkingNo = null;
        }
        //搜索
        $data = request()->get();
        //var_dump($data);
        if (!empty($data)){
            if(!empty($data['parkingNo'])){
                $where['parkingNo'] = $data['parkingNo'];//停车场名称
            }else{
                $where['parkingNo'] = "";
            }
        }else{
            $where=array();
        }
        $where=array_filter($where);
        $export = $where;
        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        $this->assign('export',$export);
        $page = request()->has('page') ? request()->get('page') : 1;

        $list =Db::name('car_coupon_merchant')->where('parkingNo','in',$parkingNo)->where($where)->order("id desc")->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
        $number = Db::name('car_coupon_merchant')->where('parkingNo','in',$parkingNo)->where($where)->count();
        $this->assign('number',$number);
        $this->assign('list',$list);
        return $this->fetch("merchant_list");
    }

    public function merchantAdd()
    {
        if(request()->isPost()){
            $data=request()->post();

            //查询该商家是否已经添加
            $carinfo=Db::table("car_coupon_merchant")->where("shops_name",$data["shops_name"])->where("parkingNo",$data["parkingNo"])->find();
            if(!empty($carinfo)){
                return "该商家已经添加";
            }
            $parkinginfo=Db::table("parkinglotinfo")->where("parkingNo",$data["parkingNo"])->find();
            $data["parkingName"]=$parkinginfo["parkingName"];
            $data["create_name"]=session("name");
            $data["create_time"]=time();
            $res=Db::table("car_coupon_merchant")->insert($data);
            if($res){
                return "添加成功";
            }else{
                return "添加失败";
            }
        }else{
            //根据当前账号查询区域
            $com = (new Common())->pidTokenId();
            $whe=new Where();
            $whe['community_id'] = array('in',implode(',',$com['community_id']));
            //根据账号查询停车场
            $parkinglist = (new orderService())->parkingList($whe);
            $this->assign('parkinglist',$parkinglist);
            return $this->fetch("merchant_add");
        }
    }


    public function couponList()
    {
        //根据当前账号查询区域
        $com = (new Common())->pidTokenId();
        $whe=new Where();
        $whe['community_id'] = array('in',implode(',',$com['community_id']));
        //根据账号查询停车场
        $parkinglist = (new orderService())->parkingList($whe);
        $this->assign('parkinglist',$parkinglist);
        //查询停车场ID
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $parkingNo[] = $park['parkingNo'];
            }
            //将数组转换成字符串
            $parkingNo = implode(',',$parkingNo);
        }else{
            $parkingNo = null;
        }
        //搜索
        $data = request()->get();
		//var_dump($data);
        if (!empty($data)){
            if(!empty($data['parkingNo'])){
                $where['parkingNo'] = $data['parkingNo'];//停车场名称
            }else{
                $where['parkingNo'] = "";
            }
        }else{
            $where=array();
        }
		$where=array_filter($where);
		$export = $where;
        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        $this->assign('export',$export);
        $page = request()->has('page') ? request()->get('page') : 1;
        
        $list =Db::name('car_coupon')->where('parkingNo','in',$parkingNo)->where($where)->order("id desc")->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
        $number = Db::name('car_coupon')->where('parkingNo','in',$parkingNo)->where($where)->count();
        $this->assign('number',$number);
        $this->assign('list',$list);
        return $this->fetch("couponlist");
    }
    public function couponAdd()
    {
        if(request()->isPost()){
           $data=request()->post();
           $data["start_time"]=strtotime($data["start_time"]);
           $data["end_time"]=strtotime($data["end_time"]);
           //查询车牌是否已经添加
           $carinfo=Db::table("car_coupon")->where("carPlateNo",$data["carPlateNo"])->where("parkingNo",$data["parkingNo"])->find();
           if(!empty($carinfo)){
               return "改车牌已经添加";
           }
           $parkinginfo=Db::table("parkinglotinfo")->where("parkingNo",$data["parkingNo"])->find();
           $data["parkingName"]=$parkinginfo["parkingName"];
           $data["create_name"]=session("name");
           $res=Db::table("car_coupon")->insert($data);
           if($res){
               return "添加成功";
           }else{
               return "添加失败";
           }
        }else{
            //根据当前账号查询区域
            $com = (new Common())->pidTokenId();
            $whe=new Where();
            $whe['community_id'] = array('in',implode(',',$com['community_id']));
            //根据账号查询停车场
            $parkinglist = (new orderService())->parkingList($whe);
            $this->assign('parkinglist',$parkinglist);
            return $this->fetch("couponadd");
        }
    }
	
    public function couponParkList(){
        //根据当前账号查询区域
        $com = (new Common())->pidTokenId();
        $whe=new Where();
        $whe['community_id'] = array('in',implode(',',$com['community_id']));
        //根据账号查询停车场
        $parkinglist = (new orderService())->parkingList($whe);
        $this->assign('parkinglist',$parkinglist);
        //查询停车场ID
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $parkingNo[] = $park['parkingNo'];
            }
            //将数组转换成字符串
            $parkingNo = implode(',',$parkingNo);
        }else{
            $parkingNo = null;
        }
        //搜索
        $data = request()->get();
		//var_dump($data);
        if (!empty($data)){
            if(!empty($data['parkingNo'])){
                $where['parkingNo'] = $data['parkingNo'];//停车场名称
            }else{
                $where['parkingNo'] = "";
            }
        }else{
            $where=array();
        }
		$where=array_filter($where);
		$export = $where;
        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        $this->assign('export',$export);
        $page = request()->has('page') ? request()->get('page') : 1;
        
        $list =Db::name('coupon_park')->where('parkingNo','in',$parkingNo)->where($where)->order("id desc")->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
        $number = Db::name('coupon_park')->where('parkingNo','in',$parkingNo)->where($where)->count();
        $this->assign('number',$number);
        $this->assign('list',$list);
        return $this->fetch("coupon_park_list");
    }
	
	public function couponParkAdd(){
	    if(request()->isPost()){
           $coupon_data  = request()->post();
            //将数据存入数据库
            
            $coupon_data["balance"]=request()->post("totalmoney");
			$CouponAdd = Db::table("coupon_park")->insert($coupon_data);
			if($CouponAdd){
			    return "添加成功";
			}else{
			    return "添加失败";
			}
        }else{
            //根据当前账号查询区域
            $com = (new Common())->pidTokenId();
            $whe=new Where();
            $whe['community_id'] = array('in',implode(',',$com['community_id']));
            //根据账号查询停车场
            $parkinglist = (new orderService())->parkingList($whe);
            $this->assign('parkinglist',$parkinglist);
            return $this->fetch("coupon_park_add");
        }
	}
	
	public function couponParkOrderList(){
	    
	}
}